//
//  MMActionView.m
//  MemeMaker
//
//  Created by Phan Quang Ha on 7/25/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMActionView.h"

@interface MMActionView ()
{
    
}
@end

@implementation MMActionView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _btnAction1 = [[UIButton alloc] initWithFrame:CGRectMake(0, -35, 35, 35)];
        _btnAction2 = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - 35, -35, 35, 35)];
        _btnAction1.hidden = YES;
        _btnAction2.hidden = YES;
        
        [_btnAction1 setBackgroundImage:[UIImage imageNamed:@"ico_cancel.png"] forState:UIControlStateNormal];
        [_btnAction2 setBackgroundImage:[UIImage imageNamed:@"ico_apply.png"] forState:UIControlStateNormal];
        
        [_btnAction1 addTarget:self action:@selector(doAnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_btnAction2 addTarget:self action:@selector(doAnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_btnAction1];
        [self addSubview:_btnAction2];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void) doAnAction:(UIButton *)btn
{
    [self setHidden:YES];
}

- (void)setHidden:(BOOL)hidden
{
    CGRect frame1 = _btnAction1.frame;
    CGRect frame2 = _btnAction2.frame;
    _btnAction1.hidden = NO;
    _btnAction2.hidden = NO;
    
    if (hidden) {
        frame1.origin.y = -35;
        frame2.origin.y = -35;
        [UIView animateWithDuration:0.3 animations:^{
            _btnAction2.frame = frame2;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                _btnAction1.frame = frame1;
            } completion:^(BOOL finished) {
                [super setHidden:hidden];
            }];
        }];
    }else{
        frame1.origin.y = 0;
        frame2.origin.y = 0;
        [super setHidden:hidden];
        [UIView animateWithDuration:0.3 animations:^{
            _btnAction1.frame = frame1;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                _btnAction2.frame = frame2;
            }];
        }];
    }
    
}

@end
